package com.cbs.android.datepick.datepickapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cbs.android.widget.calendar.CalendarView;
import com.cbs.utils.F;
import com.cbs.utils.S;

import java.util.Date;

public class CalendarViewActivity extends AppCompatActivity {
    private CalendarView calendarView;
    private FrameLayout preView;
    private FrameLayout nextView;
    private TextView monthView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar_view);

        calendarView = (CalendarView) findViewById(R.id.main_calendar_calendar);
        preView = (FrameLayout) findViewById(R.id.main_calendar_pre);
        nextView = (FrameLayout) findViewById(R.id.main_calendar_next);
        monthView = (TextView) findViewById(R.id.main_calendar_month);

        monthView.setText(F.Date.format(S.getTimeStamp(), "yyyy") + "年" + F.Date.format(S.getTimeStamp(), "MM") + "月");
        calendarView.setOnCalendarDateChangedListener(new CalendarView.OnCalendarDateChangedListener() {
            public void onCalendarDateChanged(int year, int month) {

                monthView.setText(String.format("%d年%02d月", year, month));
            }
        });
        //上月
        preView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                calendarView.lastMonth();
            }
        });
        //下月
        nextView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                calendarView.nextMonth();
            }
        });

        calendarView.setOnCalendarClickListener(new CalendarView.OnCalendarClickListener() {
            @Override
            public void onCalendarClick(int row, int col, final String dateFormat) {
                if (calendarView.isFlipping()) {
                    return;
                }
                int month = Integer.parseInt(dateFormat.substring(
                        dateFormat.indexOf("-") + 1,
                        dateFormat.lastIndexOf("-")));

                if (calendarView.getCalendarMonth() - month == 1//跨年跳转
                        || calendarView.getCalendarMonth() - month == -11) {
                    calendarView.lastMonth();

                } else if (month - calendarView.getCalendarMonth() == 1 //跨年跳转
                        || month - calendarView.getCalendarMonth() == -11) {
                    calendarView.nextMonth();

                } else {
                    calendarView.removeAllBgColor();
                    calendarView.setCalendarDayBgColor(dateFormat, R.color.cbs_calendar_splitter);
                    calendarView.notifyDataSetChanged();
                }
            }
        });
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                calendarView.removeAllMarks();
                for (int i = -30; i < 20; i++) {
                    if (i % 2 == 0) {
                        if (i == 0) {
                            calendarView.addMark(new Date((long) S.getTimeStamp() * 1000l), R.drawable.background_calendar_isrecord, R.color.white);
                        }else{
                            calendarView.addMark(new Date((long) (S.getTimeStamp() + i * 86400) * 1000l), R.drawable.background_calendar_isrecord, R.color.assist_light_purple);
                        }
                    }
                }
                calendarView.notifyDataSetChanged();
            }
        });

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                calendarView.removeAllMarks();
                calendarView.notifyDataSetChanged();
            }
        });
    }
}
